/**
 * Copyright (C), 2015-2020, bogewang
 * FileName: StrategyFactory
 * Author:   foren236598164@aliyun.com
 * Date:     2020/3/4 15:22
 * Description: 策略工厂
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.bogewang.pay.factory;

import com.bogewang.strategy.PayStrategy;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 〈一句话功能简述〉<br> 
 * 〈策略工厂〉
 *
 * @author foren236598164@aliyun.com
 * @create 2020/3/4
 * @since 1.0.0
 */
public class StrategyFactory {

    private static Map<String, PayStrategy> strategyMap = new ConcurrentHashMap<>();

    public static PayStrategy getPayStrategy(String classAddress){
        try {
            if (StringUtils.isEmpty(classAddress)){
                return null;
            }

            PayStrategy payStrategy = strategyMap.get(classAddress);
            if (payStrategy != null){
                return payStrategy;
            }

            Class<?> aClass = Class.forName(classAddress);
            payStrategy = (PayStrategy)aClass.newInstance();
            strategyMap.put(classAddress, payStrategy);
            return payStrategy;
        } catch (Exception e){
            return null;
        }
    }

}
